package com.bw.springcloud.controller;

import com.bw.springcloud.entity.CommonResult;
import com.bw.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 类描述
 *
 * @author Mr.Wu
 * @create 2020-05-08
 */
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "paymentInfo_Global_FallbackMethod")
public class OrderHystrixController {

	@Resource
	private PaymentHystrixService paymentHystrixService;


	@GetMapping("/consumer/payment/hystrix/ok/{id}")
	public String paymentInfo_OK(@PathVariable("id") Integer id){
		return paymentHystrixService.paymentInfo_OK(id);
	}

	@GetMapping("/consumer/payment/hystrix/timeout/{id}")
	@HystrixCommand(fallbackMethod = "paymentInfo_TimeoutFallbackMethod",commandProperties = {
			@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "2000")
	})
	public String paymentInfo_TimeOut(@PathVariable("id") Integer id){

		return paymentHystrixService.paymentInfo_TimeOut(id);
	}

	@GetMapping("/consumer/payment/hystrix/default/timeout/{id}")
	@HystrixCommand
	public String paymentInfo_DefaultTimeOut(@PathVariable("id") Integer id){

		return paymentHystrixService.paymentInfo_TimeOut(id);
	}


	public String paymentInfo_TimeoutFallbackMethod(@PathVariable("id") Integer id){
		return "/(ToT)/我是消费者80，调用8001支付系统繁忙，请10秒钟后重新尝试、\t";
	}

	public String paymentInfo_Global_FallbackMethod(){
		return "这是我全局返回的错误信息，请稍后再试， /(ToT)/";
	}


}
